package com.xafc.demo.security.handle;

import com.xafc.demo.framework.utils.RequestUtils;
import com.xafc.demo.security.utils.ApiResultUtil;
import com.xafc.demo.framework.result.ApiResult;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Component
public class JwtAccessDeniedHandler implements AccessDeniedHandler {

    @Override
    public void handle(HttpServletRequest request,
                       HttpServletResponse response,
                       AccessDeniedException accessDeniedException) throws IOException {
        if (RequestUtils.isAjax(request)){
            ApiResultUtil.responseJson(response, ApiResult.deny("没有权限"));
        }else {
            response.sendError(403);
        }
    }
}